<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use front\assets\AppAsset;
use yii\helpers\Url;
$this->title = '提交订单';
?>
<body class="u-bg1">
<div class="m-buy">

    <?php if($list['user_site']&&$list['user_site'][0]){ ?>
        <div class="adds" id="J-address" data-site="<?php if(isset($list['user_site'][0]['id'])){echo $list['user_site'][0]['id'];}?>">
            <table>
                <tr>
                    <td><img src="/static/img/ico11.png" /></td>
                    <td><p><span id="j-edit">收货地址</span><em id="j-addresschar"><?php if(isset($list['user_site'][0]['address'])){echo $list['user_site'][0]['address'];}?></em></p>
                        <h5><b id="j-name">收货人:  <?php if(isset($list['user_site'][0]['name'])){echo $list['user_site'][0]['name'];}?></b>
                            <span id="j-mobile"><?php if(isset($list['user_site'][0]['mobile'])){echo $list['user_site'][0]['mobile'];}?></span></h5>
                        <input type="hidden" value="" name='district' id="j-district">
                        <input type="hidden" value="" name='addr_default' id="j-addr_default">
                    </td>
                </tr>
            </table>
        </div>
    <?php }else{ ?>
        <div class="adds" id="J-address" data-site="">
            <table>
                <tr>
                    <td><img src="/static/img/ico11.png" /></td>
                    <td><p><span id="j-edit">请选择收货地址</span><em id="j-addresschar"></em></p>
                        <h5><b id="j-name"></b><span id="j-mobile"></span></h5>
                        <input type="hidden" value="" name='district' id="j-district">
                        <input type="hidden" value="" name='addr_default' id="j-addr_default">
                    </td>
                </tr>
            </table>
        </div>
    <?php } ?>


    <?php foreach($list['product'] as $k=>$v){?>
        <div class="desc order-desc" data-shop="<?php echo $v['shop_id'];?>" data-pid="<?php echo $v['product_id'];?>">
            <a href="<?= Url::to(['product/detail','pid' => $v['product_id']])?>">
                <img src="<?php if(isset($v['data']['thumb'])){ echo $v['data']['thumb'];}?>"/>
            </a>
            <h4>
                <a href="<?= Url::to(['product/detail','pid' => $v['product_id']])?>"><?php echo $v['name'];?></a><br />
                <?php echo $v['data']['item_attrText_map'];?>
                <em>数量：</em><i id="buy-num"><?php echo $v['num'];?></i>
            </h4>
            <label>￥ <b><?php echo $v['price'];?></b></label>
        </div>
    <?php } ?>

    <div class="u-swit J_switch pay-info">
        <h4><span>支付方式</span><a href="javascript:;" >线上支付</a><input value="1" type="hidden" /></h4>
        <p data-id="1" class="z-on" id="J_payOnline">线上支付</p>
        <p data-id="2" id="J_payOffline">线下支付</p>
    </div>

    <div class="u-swit J_switch express-in">
        <h4><span>配送方式</span><a href="javascript:;" class="j-express">快递</a><input value="1" id="J_deliveryType" type="hidden" /></h4>
        <p data-id="1" class="z-on" id="J_delivery">快递</p>
        <p data-id="4" id="J_address-self" style="display: none">自提</p>
    </div>

    <div class="u-swit J_switch express-time u-swit-spe">
        <h4><span>送货时间</span><a href="javascript:;">只双休日、节假日送货</a><input value="3" type="hidden" /></h4>
        <p data-id="1">只工作日送货</p>
        <p data-id="2">工作日、双休日、节假日送货</p>
        <p data-id="3" class="z-on">只双休日、节假日送货</p>
    </div>

    <?php if($coupons){?>
        <h5 class="u-fat" id="J_coupon"><span>优惠券</span><a href="javascript:;">-￥220.00</a></h5>
    <?php }?>
    <h5 class="u-fat" id="J_remark"><span>备注</span><a href="javascript:;"></a></h5>

    <ul class="m-price">
        <li><b>总价</b><span>￥<?php echo $list['total_price']?></span></li>
<!--        <li><b>优惠</b><span>- ￥220.00</span></li>-->
<!--        <li><b>运费</b><span>+ ￥0.00</span></li>-->
    </ul>

    <dl class="m-resu f-cb">
        <dt>您需要支付：<b>￥<?php echo $list['total_price']?></b></dt>
        <dd><a href="javascript:;" class="orderBuy"  data-buy="<?php echo $hide_buy;?>">购买</a></dd>
    </dl>
</div>


 <div class="m-trans">
    <div class="cnt" id="J_trans_coupon">
        <div class="m-coup J_sltCnt">
            <?php foreach($coupons as $item) { ?>
                <?php  if ($item['type'] == 2 || $item['type'] == 3) {?>
                    <?php if ($item['coupons_status'] == 3) {?>
                        <div class="f-cb u-spe2">
                            <img src="/static/img/bg02.png" />
                            <span>代金券</span>
                            <h4><b><?=$item['groupInfo']['voucher']['value']?></b>元<br /><small>(消费满<?=$item['groupInfo']['voucher']['must_voucher_price'] ?>可用)</small><br /><em>有效期至<?=date('Y-m-d',$item['groupInfo']['end_time'])?></em></h4>
                            <h5><b>可使用</b><br /><small>(剩余 <?php $time = $item['groupInfo']['end_time'] - time(); echo intval($time/86400); ?>天)</small></h5>
                        </div>
                    <?php }?>
                    <?php if ($item['coupons_status'] == 4) {?>
                        <div class="f-cb u-spe2">
                            <img src="/static/img/bg02.png" />
                            <span>代金券</span>
                            <h4><b><?=$item['groupInfo']['voucher']['value']?></b>元<br /><small>(消费满<?=$item['groupInfo']['voucher']['must_voucher_price'] ?>可用)</small><br /><em>有效期至<?=date('Y-m-d',$item['groupInfo']['end_time'])?></em></h4>
                            <h5><b>已使用</b></h5>
                        </div>
                    <?php }?>
                    <?php if ($item['coupons_status'] == 5) {?>
                        <div class="f-cb u-spe2">
                            <img src="/static/img/bg02.png" />
                            <span>代金券</span>
                            <h4><b><?=$item['groupInfo']['voucher']['value']?></b>元<br /><small>(消费满<?=$item['groupInfo']['voucher']['must_voucher_price'] ?>可用)</small><br /><em>有效期至<?=date('Y-m-d',$item['groupInfo']['end_time'])?></em></h4>
                            <h5><b>已过期</b></h5>
                        </div>
                    <?php }?>
                <?php }?>

                <?php  if ($item['type'] == 0 || $item['type'] == 1) {?>
                    <?php if ($item['coupons_status'] == 3) {?>
                        <div class="f-cb u-spe1">
                            <img src="/static/img/bg01.png" />
                            <span>折扣券</span>
                            <h4><b><?=$item['groupInfo']['discount']['value']?></b>折<br /><small>(消费满<?=$item['groupInfo']['discount']['must_discount_price'] ?>可用)</small><br /><em>有效期至<?=date('Y-m-d',$item['groupInfo']['end_time'])?></em></h4>
                            <h5><b>可使用</b><br /><small>(剩余 <?php $time = $item['groupInfo']['end_time'] - time(); echo intval($time/86400); ?>天)</small></h5>
                        </div>
                    <?php }?>
                    <?php if ($item['coupons_status'] == 4) {?>
                        <div class="f-cb u-spe1">
                            <img src="/static/img/bg01.png" />
                            <span>折扣券</span>
                            <h4><b><?=$item['groupInfo']['discount']['value']?></b>折<br /><small>(消费满<?=$item['groupInfo']['discount']['must_discount_price'] ?>可用)</small><br /><em>有效期至<?=date('Y-m-d',$item['groupInfo']['end_time'])?></em></h4>
                            <h5><b>已使用</b></h5>
                        </div>
                    <?php }?>
                    <?php if ($item['coupons_status'] == 5) {?>
                        <div class="f-cb u-spe1">
                            <img src="/static/img/bg01.png" />
                            <span>折扣券</span>
                            <h4><b><?=$item['groupInfo']['discount']['value']?></b>折<br /><small>(消费满<?=$item['groupInfo']['discount']['must_discount_price'] ?>可用)</small><br /><em>有效期至<?=date('Y-m-d',$item['groupInfo']['end_time'])?></em></h4>
                            <h5><b>已过期</b></h5>
                        </div>
                    <?php }?>
                <?php }?>
            <?php } ?>
         </div>
        <div class="m-coup J_sltCnt">

        </div>
    </div>
</div>


<div class="m-trans">
    <div class="cnt m-remark" id="J_trans_remark"">
        <em contenteditable="true"></em>
        <a href="javascript:;"><span>确定</span></a>
    </div>
</div>

<form action="<?=url::to('create')?>" method="post" id="orderform">

</form>

<form action="/web/address/self-address?from=submit" method="post" id="subform">

</form>

<div class="m-transX" id="J_addAddress">

    <iframe class="m-iframe" src="/web/order/addaddr" style="" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>

</div>

<div class="m-transX" id="J_addAddressSelf">

    <iframe class="m-iframe" src="/web/order/addaddrself" style="" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>

</div>

<script>

    $(function(){


        // 滑动的基础样式
        var transX = 'z-transX';

//        $('#J-address').click(function(){
//            getaddr();
//        })
        // 展开 收起
        var switchTag = $('.J_switch'), zon = 'z-on';
        switchTag.on('tap', 'a', function(){

            var _that = $(this), _parent = _that.parent('h4');

            if(_parent.hasClass(zon)){
                _parent.removeClass(zon).siblings().hide();
            }else{
                _parent.addClass(zon).siblings().show();
            }

            var _idx = switchTag.find('a').index(_that);

            // 点击配送方式
            if(_idx === 1){

                var _v = $('.pay-info input').val();

                if(_v === '1'){
                    $('#J_address-self').hide();
                }else{
                    $('#J_address-self').show();
                }
            }

        });

        // 选择
        switchTag.on('tap', 'p', function(){

            var _that = $(this), _parent = _that.parent('div'), _id = _that.attr('data-id');

            if(_that.hasClass(zon)){
                _parent.find('p').hide();
                _parent.find('h4').removeClass(zon);
                return false;
            }else{
                _that.addClass(zon).siblings().removeClass(zon);
                _parent.find('a').text(_that.text()).next().val(_that.attr('data-id'));
                _parent.find('h4').removeClass(zon).siblings().hide();

                if(_id === '4'){
                    $('#J_addAddressSelf').addClass(transX);
                }
            }

            $('#J_delivery').hide();
            $('#J_address-self').hide().parent().find('h4').removeClass(zon);


            if(_that.attr('id')  === 'J_payOnline' && $('#J_deliveryType').val() === '4'){
                $('.j-express').text('快递').next().val(1);
                $('#J_delivery').addClass(zon);
            }

        });

        // Tab切换
        var sltBar = $('#J_sltBar').children('span'),
            sltCnt = $('.J_sltCnt'),
            zon = 'z-on';
        sltBar.on('touchend', function(evt){

            var _that = $(this);

            if(_that.hasClass(zon)) return !1;

            var _i = _that.index();

            sltBar.removeClass(zon).eq(_i).addClass(zon);

            var _html = '';

            // 动态加载数据
            $.ajax({ url: 'ajax.coupon.php?funcName=?', dataType:'jsonp', data:{'type':_i}, timeout:5000,

                success: function(_info){

                    console.log(_info);

                    _html = createList(_info);

                    sltCnt.hide().eq(_i).html(_html).show();
                }
            });

            evt.preventDefault();
            return false;
        });

        // 显示优惠券滑板
        var couponId = 'J_trans_coupon';
        $('#J_coupon').on('tap', function(){
            setTrans(couponId, $(window).width());
        });

        // 选择优惠券 隐藏 滑板
        sltCnt.on('touchend', function(evt){
            setTrans(couponId, 0);
            evt.preventDefault();
            return false;
        });

        // 显示 修改备注 滑板
        var remarkId = 'J_trans_remark';
        $('#J_remark').on('tap', function(){
            setTrans(remarkId, $(window).width());
        });

        //地址
        // transform
        $('#J-address').on('tap', function(){
            $('#J_addAddress').addClass(transX);
        });

        var remarktext;
        // 修改备注 隐藏 滑板
        $('#'+remarkId).on('touchend', 'a', function(evt){
            remarktext = $(this).parents('.m-remark').find('em').text();
            $('#J_remark a').text(remarktext);
            setTrans(remarkId, 0);
            evt.preventDefault();
            return false;
        });

        //提交订单
        var shop_id=[],pid=[],address,pay,express,express_time,remark,html='';
        $('.m-resu').tap(function(){
            //商品的信息
            $('.order-desc').each(function(k,v){
                html+='<input type="hidden" id="shop_id" name="shop_id['+k+']" value="'+$(this).attr('data-shop')+'">';
                html+='<input type="hidden" id="pid" name="pid['+k+']" value="'+$(this).attr('data-pid')+'">';
            })
            address=$('.adds').attr('data-site');
            if(!address){
                alert('请选择收货地址');
                return false;
            }
            var isdefault = $('#j-addr_default').val();

            html+='<input type="hidden" id="hide_buy" name="hide_buy" value="'+$('.orderBuy').attr('data-buy')+'">';
            html+='<input type="hidden" id="isdefaddress" name="isdefaddress" value="'+isdefault+'">';
            html+='<input type="hidden" id="buy_num" name="buy_num" value="'+$('#buy-num').text()+'">';
            //获取地址信息
            html+='<input type="hidden" id="address" name="address" value="'+$('.adds').attr('data-site')+'">';
            //支付方式
            html+='<input type="hidden" id="pay" name="pay" value="'+$('.pay-info h4 input').val()+'">';
            //快递方式
            html+='<input type="hidden" id="express" name="express" value="'+$('.express-in a').text()+'">';

            html+='<input type="hidden" id="express_self" name="express_self" value="'+$('.express-in input').val()+'">';//自提
            //送货时间
            html+='<input type="hidden" id="express_time" name="express_time" value="'+ $('.express-time a').text()+'">';
            //备注

            html+='<input type="hidden" id="remark" name="remark" value="'+  $('#J_remark a').text()+'">';

            $("#orderform").append(html);
            $("#orderform").submit();
        })

    });


    // 操作滑板
    function setTrans(_id, _x){

        var _vendor = '';
        var _style = document.createElement('div').style;
        var _vdr = ['-webkit-', '-moz-', '-o-', '-ms-'];
        for(var i in _vdr){
            var _v = _vdr[i] + 'transform';
            if(_style[_v] !== undefined) _vendor = _vdr[i];
        }

        $('#'+ _id).parent().css({'visibility':(_x === 0 ? 'hidden' : 'visible')});
        document.getElementById(_id).style.cssText = _vendor + 'transition:all .25s linear;'+ _vendor +'transform:translate3d(-'+ _x +'px, 0px, 0px);';
        $('#'+ _id).height($('#'+ _id).parent().height());
    }


    // 动态创建列表
    function createList(info){

        var _html = '';

        for(var i in info){

            var rs = info[i];

            var canUsed = '已使用';
            if(rs['leave'] > 0){
                canUsed = '可使用';
            }else if(rs['leave'] == 0){
                canUsed = '已过期';
            }
            var leave = rs['leave'] > 0 ? '<small>(剩余 '+ rs['leave'] +' 天)</small>' : '';

            _html += '<div class="f-cb u-spe'+ rs['class'] +'">' +
                '<img src="img/bg0'+ rs['class'] +'.png" />' +
                '<span>'+ rs['type'] +'</span>' +
                '<h4><b>'+ rs['money'] +'</b>元<br /><small>('+ rs['desc'] +')</small><br /><em>'+ rs['date'] +'</em></h4>' +
                '<h5><b>'+ canUsed +'</b><br />'+ leave +'</h5>' +
                '</div>';
        }

        if(_html === '') _html = '<h6>您还没有优惠券</h6>';

        return _html;
    }



</script>

</body>